package com.tm.controller;

import com.tm.model.Person;
import com.tm.model.Serial;
import com.tm.service.PersonService;
import com.tm.util.RestReturnModel;
import com.tm.vo.PersonVo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import org.apache.poi.xssf.usermodel.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;


@Api(tags = "用户接口")
@CrossOrigin
@RestController
@RequestMapping("person")
public class PersonController {

    @Autowired
    private PersonService personService;
    //新增
    @ApiOperation(value = "新增用户",notes = "新增用户" )
    @PostMapping
    public Map<String,Object> addPerson(@RequestBody Person person){
            Map<String,Object> map = new HashMap<>();
        try {
            personService.addPerson(person);
            map.put("code",200);
            map.put("result","success");
        } catch (Exception e){
            e.printStackTrace();
            map.put("code",3254);
        }
        return map;
    }

    //修改
    @ApiOperation(value = "修改用户",notes = "修改用户" )
    @PutMapping
    public Map<String, Object> updatePerson(@RequestBody Person person){
        Map<String, Object> map = new HashMap<>();
        try {
            personService.updatePerson(person);
            map.put("code",200);
            map.put("result","success");
        } catch (Exception e){
            e.printStackTrace();
            map.put("code",3254);
        }
        return map;
    }

    //回显
    @GetMapping("/{personId}")
    public Map<String, Object> queryPersonById(@PathVariable Integer personId){
        Map<String, Object> map = new HashMap<>();
        try {
            Person person = personService.queryPersonById(personId);
            map.put("code",200);
            map.put("result", person);
        } catch (Exception e) {
            e.printStackTrace();
            map.put("code","3412");
        }
        return map;
    }


    //分页+条件查询
    @ApiOperation(value = "获取所有数据+条件+分页",notes = "获取用户数据" )
    @ApiImplicitParam(name = "personVo",value = "用户id",required = true,dataType = "number")
    @GetMapping
    public Map<String, Object> queryPerson(PersonVo personVo){
        Map<String, Object> map = new HashMap<>();
        try {
            personVo = personService.queryPerson(personVo);
            map.put("code",200);
            map.put("result",personVo);
        }catch (Exception e){
            e.printStackTrace();
            map.put("code",456);    //代表查询失败
        }
        return map;
    }

    @ApiOperation(value = "删除用户",notes = "删除用户" )
    @ApiImplicitParam(name = "personVo",value = "用户id",required = true,dataType = "number")
    //删除
    @DeleteMapping("/{personId}")
    public Map<String, Object> deletePersonId(@PathVariable Integer personId){
        Map<String, Object> map = new HashMap<>();
        try {
            personService.deletePersonId(personId);
            map.put("code",200);
            map.put("result" ,"success");
        }catch (Exception  e){
            map.put("code",3132);
        }
        return map;
    }

    @ApiOperation(value = "导出")
    @GetMapping("exportFile")
    public void allPOIPerson(HttpServletResponse response ){
        personService.allPOIPerson(response);
    }

    @ApiOperation(value = "导入")
    @PostMapping ("uploadFile")
    public void uploadFile(MultipartFile dataFile) {
        personService.uploadFile(dataFile);
    }


}
